package io.github.lee.jel.repository

import io.github.lee.jel.dao.JellyfinSessionDao
import io.github.lee.jel.domain.po.JellyfinSession
import io.github.lee.jel.manager.JellyfinSessionManager
import javax.inject.Inject

class JellyfinSessionRepository @Inject constructor(
    val dao: JellyfinSessionDao
) : BaseRepository() {
    fun queryByAddress(address: String): JellyfinSession? {
        return dao.queryByAddress(address)
    }

    fun queryCurrentSession(): JellyfinSession? =
        dao.queryCurrentSession()


    fun replaceSession(session: JellyfinSession) {
        val oldSession = queryCurrentSession()
        if (null != oldSession) {
            oldSession.current = false
            dao.update(oldSession)
        }
        dao.insertAbort(session)
        JellyfinSessionManager.postSession(session)
    }


    fun queryAll(): List<JellyfinSession> =
        dao.queryAll()

    fun delete(session: JellyfinSession?): Int {
        if (null == session) {
            return -1
        }
        val result = dao.delete(session)

        JellyfinSessionManager.postSessionDelete(session)
        return result
    }
}